package com.app.hmst.main;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HouseInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

@SuppressLint({ "NewApi", "ValidFragment" }) public class GeneralInfoForNewHomeFragment extends Fragment{
	private View v;
	private SlidingMenu _sm;
	private ImageView menu_icon,saveGenInfo;
	//Mock Name of spinner
	private Spinner spinProvince,spinAumphur,spinTumbon;
	private HomestayInfoVO homestayInfoVO;
	private EditText title,telephone,fax,website,email,contact;
	private HouseInfoVO houseInfoVO;
	
	public GeneralInfoForNewHomeFragment(SlidingMenu sm,HomestayInfoVO homestayInfoVO,HouseInfoVO houseInfoVO){
		this._sm = sm;
		this.homestayInfoVO = homestayInfoVO;
		this.houseInfoVO = houseInfoVO;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		System.gc();
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		StrictMode.setThreadPolicy(policy);
		
		v = inflater.inflate(R.layout.fragment_generalinfo_page_for_new,container, false);
		
		spinProvince = (Spinner) v.findViewById(R.id.spinner1);
		spinAumphur = (Spinner) v.findViewById(R.id.spinner2);
		spinTumbon = (Spinner) v.findViewById(R.id.spinner3);
		saveGenInfo = (ImageView) v.findViewById(R.id.imageView13);
		
		title = (EditText) v.findViewById(R.id.editText1);
		telephone = (EditText) v.findViewById(R.id.editText4); 
		
		fax = (EditText) v.findViewById(R.id.editText6);
		
		website = (EditText) v.findViewById(R.id.EditText03);
		
		email = (EditText) v.findViewById(R.id.EditText04);
		
		contact = (EditText) v.findViewById(R.id.EditText05); 
		
		
		if(houseInfoVO==null){
		telephone.setText(homestayInfoVO.tel);
		fax.setText(homestayInfoVO.fax);
		website.setText(homestayInfoVO.website);
		email.setText(homestayInfoVO.email);
		contact.setText(homestayInfoVO.contactName);
		}else{
			title.setText(houseInfoVO.name);
			telephone.setText(houseInfoVO.tel);
			fax.setText(houseInfoVO.fax);
			website.setText(houseInfoVO.website);
			email.setText(houseInfoVO.email);
			contact.setText(houseInfoVO.contactName);	
		}
//		//Sample province for checking size of adapter
//		ArrayList<String> mockProvince = new ArrayList<String>();
//		mockProvince.add(homestayInfoVO.province);
//		ArrayAdapter<String> provinceAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockProvince);
//		provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		spinProvince.setAdapter(provinceAdapter);
//		
//		ArrayList<String> mockAumphur = new ArrayList<String>();
//		mockAumphur.add(homestayInfoVO.district);
//		ArrayAdapter<String> aumphurAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockAumphur);
//		aumphurAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		spinAumphur.setAdapter(aumphurAdapter);
//		
//		ArrayList<String> mockTumbon = new ArrayList<String>();
//		mockTumbon.add(homestayInfoVO.tumbol);
//		ArrayAdapter<String> tumbonAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,mockTumbon);
//		tumbonAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//		spinTumbon.setAdapter(tumbonAdapter);
		
		saveGenInfo.setOnClickListener(new SaveGenInfoListener());
		menu_icon = (ImageView) v.findViewById(R.id.imageView6);
		menu_icon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				_sm.toggle();
			}
		});
		return v;
	}
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}
	public class SaveGenInfoListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(!"".equals(title.getText().toString())){
//				HouseInfoVO tempHouseInfoVO = null;
				if(houseInfoVO==null){
					
					houseInfoVO = new HouseInfoVO();
					int id = houseInfoVO.getPk();
					houseInfoVO.id = houseInfoVO.getPk();
					houseInfoVO.name = title.getText().toString();
					houseInfoVO.tel = telephone.getText().toString();
					houseInfoVO.fax = fax.getText().toString();
					houseInfoVO.website = website.getText().toString();
					houseInfoVO.email = email.getText().toString();
					houseInfoVO.homestayId = homestayInfoVO.id+"";
					houseInfoVO.newHouse = "T";
					houseInfoVO.contactName = contact.getText().toString();
					houseInfoVO.save();
					houseInfoVO.id = id;
					getFragmentManager().beginTransaction().replace(R.id.menu_frame, new MenuFragment(_sm,true,homestayInfoVO)).commit();
					switchFragment(new EvaluateFormFragment(_sm, homestayInfoVO,houseInfoVO,false));
					
					
					
				}else{
				houseInfoVO.save();
				getFragmentManager().beginTransaction().replace(R.id.menu_frame, new MenuFragment(_sm,true,homestayInfoVO)).commit();
				switchFragment(new EvaluateFormFragment(_sm, homestayInfoVO,houseInfoVO,false));
				}
			}
		}
		
	}
	public class InitAndLoadData extends AsyncTask<String, Void, List<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(getActivity(),"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<String> doInBackground(String... params) {
			// TODO Auto-generated method stub			
			List<String> list = new ArrayList<String>();
			list.add("000");
			return list;
		}
		
		@Override
		protected void onPostExecute(List<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}


	}
}
